package by.imotaeva.printing_services.services.impl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.imotaeva.printing_services.dataaccess.CustomerDao;
import by.imotaeva.printing_services.datamodel.Customer;
import by.imotaeva.printing_services.services.CustomerService;

@Service
public class CustomerServiceImpl implements CustomerService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(CustomerServiceImpl.class);

	@Inject
	private CustomerDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of CustomerService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public Customer get(Long id) {
		Customer entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Customer entity) {
		if (entity.getId() == null) {
			LOGGER.debug("Save new: {}", entity);
			dao.insert(entity);
		} else {
			LOGGER.debug("Update: {}", entity);
			dao.update(entity);
		}

	}

	@Override
	public void delete(Customer customer) {
		LOGGER.debug("Remove: {}", customer);
		dao.delete(customer.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all customers");
		dao.deleteAll();

	}

}
